package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.AuditFlowDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 实例审批流程 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-01-01
 */
public interface AuditFlowDetailMapper extends BaseMapper<AuditFlowDetail> {

    @Select("select count(id) as count from audit_flow_detail where sourse_id = ${sourseId} and sourse_version_num = ${sourseVersionNum}")
    public Integer getVersionCount(@Param("sourseId") Long sourseId,@Param("sourseVersionNum") Integer sourseVersionNum);

    /**
     *
     * @param sourseId 审批实例id
     * @param teacherId 审批老师id
     * @param sourseVersionNum 版本id
     * @param type 1：审批，2：抄送
     * @return
     */
    @Select("select * from audit_flow_detail where sourse_id = ${sourseId} and check_id = ${checkId} and sourse_version_num = ${sourseVersionNum} and step_num = ${stepNum} and type = ${type} limit 1")
    public AuditFlowDetail getFlowDetailOne(@Param("sourseId") Long sourseId,@Param("checkId") Long teacherId,@Param("stepNum") Integer stepNum,@Param("sourseVersionNum") Integer sourseVersionNum,@Param("type") Integer type);

    /**
     *
     * @param sourseId 申请的实例id
     * @param sourseVersionNum 版本
     * @param nowStepNum 当前审批到的步骤
     * @param type 1：审批类型，2抄送
     * @return
     */
    @Select("select * from audit_flow_detail where sourse_id = ${sourseId}  and sourse_version_num = ${sourseVersionNum} and type = ${type} and step_num > ${nowStepNum} ORDER BY step_num asc limit 1")
    public AuditFlowDetail getNextFlowDetail(@Param("sourseId") Long sourseId,@Param("sourseVersionNum") Integer sourseVersionNum,@Param("nowStepNum") Integer nowStepNum,@Param("type") Integer type);


    /**
     *
     * @param sourseId 申请的实例id
     * @param sourseVersionNum 版本
     * @param nowStepNum 当前审批到的步骤
     * @param type 1：审批类型，2抄送
     * @return
     */
    @Select("select * from audit_flow_detail where sourse_id = ${sourseId}  and sourse_version_num = ${sourseVersionNum} and type = ${type} and step_num = ${nowStepNum} ORDER BY step_num asc limit 1")
    public AuditFlowDetail getNowStepFlowDetail(@Param("sourseId") Long sourseId,@Param("sourseVersionNum") Integer sourseVersionNum,@Param("nowStepNum") Integer nowStepNum,@Param("type") Integer type);


    @Select("select id,step_num,check_num,`name`,type,`status`,GROUP_CONCAT(`status`) as `status_str`,is_arrive,GROUP_CONCAT(check_id) as check_ids from audit_flow_detail where sourse_id = ${sourseId} and sourse_version_num = ${sourseVersionNum} GROUP BY step_num ORDER BY step_num asc")
    public List<AuditFlowDetail> getFlowDetailForSourseId(@Param("sourseId") Long sourseId,@Param("sourseVersionNum") Integer sourseVersionNum);

    /**
     *获取抄送人
     * @param sourseId 申请的实例id
     * @param sourseVersionNum 版本
     * @param type 1：审批类型，2抄送
     * @return
     */
    @Select("select * from audit_flow_detail where sourse_id = ${sourseId}  and sourse_version_num = ${sourseVersionNum} and type = ${type} and is_arrive =${isArrive} ORDER BY step_num")
    public List<AuditFlowDetail> getCopyFlowDetailList(@Param("sourseId") Long sourseId,@Param("sourseVersionNum") Integer sourseVersionNum,@Param("type") Integer type,@Param("isArrive") Integer isArrive);
}
